
package hairmember

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/hairmember"
    hairmemberReq "github.com/flipped-aurora/gin-vue-admin/server/model/hairmember/request"
)

type MemberLevelsService struct {}
// CreateMemberLevels 创建memberLevels表记录
// Author [yourname](https://github.com/yourname)
func (memberLevelsService *MemberLevelsService) CreateMemberLevels(ctx context.Context, memberLevels *hairmember.MemberLevels) (err error) {
	err = global.GVA_DB.Create(memberLevels).Error
	return err
}

// DeleteMemberLevels 删除memberLevels表记录
// Author [yourname](https://github.com/yourname)
func (memberLevelsService *MemberLevelsService)DeleteMemberLevels(ctx context.Context, id string) (err error) {
	err = global.GVA_DB.Delete(&hairmember.MemberLevels{},"id = ?",id).Error
	return err
}

// DeleteMemberLevelsByIds 批量删除memberLevels表记录
// Author [yourname](https://github.com/yourname)
func (memberLevelsService *MemberLevelsService)DeleteMemberLevelsByIds(ctx context.Context, ids []string) (err error) {
	err = global.GVA_DB.Delete(&[]hairmember.MemberLevels{},"id in ?",ids).Error
	return err
}

// UpdateMemberLevels 更新memberLevels表记录
// Author [yourname](https://github.com/yourname)
func (memberLevelsService *MemberLevelsService)UpdateMemberLevels(ctx context.Context, memberLevels hairmember.MemberLevels) (err error) {
	err = global.GVA_DB.Model(&hairmember.MemberLevels{}).Where("id = ?",memberLevels.Id).Updates(&memberLevels).Error
	return err
}

// GetMemberLevels 根据id获取memberLevels表记录
// Author [yourname](https://github.com/yourname)
func (memberLevelsService *MemberLevelsService)GetMemberLevels(ctx context.Context, id string) (memberLevels hairmember.MemberLevels, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&memberLevels).Error
	return
}
// GetMemberLevelsInfoList 分页获取memberLevels表记录
// Author [yourname](https://github.com/yourname)
func (memberLevelsService *MemberLevelsService)GetMemberLevelsInfoList(ctx context.Context, info hairmemberReq.MemberLevelsSearch) (list []hairmember.MemberLevels, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&hairmember.MemberLevels{})
    var memberLevelss []hairmember.MemberLevels
    // 如果有条件搜索 下方会自动创建搜索语句
    
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }

	err = db.Find(&memberLevelss).Error
	return  memberLevelss, total, err
}
// GetMemberLevelsPublic 获取所有会员等级数据（不需要鉴权）
// Author [yourname](https://github.com/yourname)
func (memberLevelsService *MemberLevelsService)GetMemberLevelsPublic(ctx context.Context) (list []hairmember.MemberLevels, err error) {
    // 查询所有状态为启用的会员等级，并按等级编码升序排序
    err = global.GVA_DB.Where("status = ?", 1).Order("level_code asc").Find(&list).Error
    return list, err
}
